package com.xiaoyuanxing.demo.mapper;

import com.github.pagehelper.Page;
import com.xiaoyuanxing.demo.entity.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

/*
 **@author LuoShengYing
 **@date 2021/5/22
 */
@Mapper
public interface OrderMapper {

    // 通过订单ID查询对应的Order数据
    @Select("select * from `order` where `orderID` = #{orderID}  order by `createdTime` DESC")
    Order selectByOrderID(@Param("orderID") int orderID);

    // 通过creatorID查询对应的Order数组
    @Select("select *  from `order` where `creatorID` = #{creatorID}  order by `createdTime` DESC")
    Page<Order> selectListByCreatorID(@Param("creatorID") long creatorID);

    // 通过订单数组IDs查询对应的Order数组
    Page<Order> selectListByOrderIDs(List<Integer> IDs);

    // 通过订单status，查询对应的Order数组，并以最新创建时间进行排序
    @Select("select * from `order` where `status` = #{status} order by `createdTime` DESC")
    Page<Order> selectListByStatus(@Param("status") int status);

    // 新增一条订单数据，并返回自增的id到传入的对象中
    @Insert("insert `order`" +
            "set `creatorID` = #{creatorID}, `comment` = #{comment}, `address` = #{address}, `price` = #{price}, `createdTime` = #{createdTime}")
    void insertOrder(Order order);

    // 更新订单状态status
    @Update("update `order`" +
            "set `status` = #{status} where `orderID` = #{orderID}")
    boolean updateStatus(@Param("orderID") int orderID, @Param("status") int status);

    // 更新订单完成时间
    @Update("update `order`" +
            "set `completedTime` = #{completedTime} where `orderID` = #{orderID}")
    boolean updateCompletedTime(@Param("orderID") int orderID, @Param("completedTime") java.sql.Timestamp completedTime);

    // 删除订单
    @Delete("delete from `order` where `orderID` = #{orderID}")
    boolean deleteByOrderID(@Param("orderID") int orderID);

}
